<?php
/**
 * Created by PhpStorm.
 * User: DongYao
 * Date: 2017/11/28
 * Time: 下午2:22
 */

namespace XmtApp\Modules\Admin\Controllers\Api;


use App\Http\Requests\AdminPost;
use App\Http\Resources\UserResource;
use App\Http\Resources\UsersResource;
use App\Tables\User;
use Illuminate\Http\Request;
use XmtApp\Modules\Admin\Controllers\Controller;

class AdminController extends Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 查询后台用户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $user = new User();

        $data = $user->backend()->withRole(['admin'])->paginate($request->header('pageSize'));

        return new UsersResource($data);
    }

    /**
     * 添加后台用户
     * @param AdminPost $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(AdminPost $request)
    {
        $data = $request->all(['name', 'password', 'sex', 'is_effect', 'source', 'email', 'nickname', 'realname', 'mobile']);

        $user = new User($data);
        $user->password = bcrypt($data['password']);
        $user->type = 1;
        $res = $user->save();

        if ($res) {
            return response()->json(ajax_return(true, '添加成功', new UserResource($user)));
        } else {
            return response()->json(ajax_return(false, '添加失败', []));
        }
    }

    /**
     * 修改后台用户信息
     * @param $id
     * @param AdminPost|Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update($id, AdminPost $request)
    {
        $data = $request->all(['email', 'nickname', 'realname', 'mobile']);

        $table = new User();

        $res = $table->where('id', $id)->update($data);

        if ($res) {
            return response()->json(ajax_return(true, '修改成功', []));
        } else {
            return response()->json(ajax_return(false, '修改失败', []));
        }
    }


    /**
     * 设置后台用户权限
     * @param $user_id
     * @param $role_ids
     * @return \Illuminate\Http\JsonResponse
     */
    public function roleAttach ($user_id, $role_ids)
    {
        $user = User::find($user_id);

        if (empty($user)) {
            return response()->json(ajax_return(false, '用户信息不存在', []));
        }


        $roles = array_filter(explode(',', $role_ids));
        $user->detachRoles();
        $user->roles()->attach($roles);

        return response()->json(ajax_return(true, '修改权限信息成功', new UserResource($user)));
    }

    /**
     * 设置后台账户是否有效
     * @param $id
     * @param $status
     * @return \Illuminate\Http\JsonResponse
     * @internal param UserPost $request
     */
    public function toggleStatus($id, $status)
    {
        $table = new User();

        $res = $table->where('id', $id)->update(['is_effect' => $status]);

        if ($res) {
            return response()->json(ajax_return(true, '设置成功'));
        } else {
            return response()->json(ajax_return(false, '设置失败'));
        }
    }
}